package com.lovo.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.commom.CscRemote.ICscUserRemote;
import com.lovo.commom.util.ImageysUtils;
import com.lovo.commom.util.MD5Encryptor;
import com.lovo.web.bean.UserBean;
import com.lovo.web.bean.UserInfoBean;
import com.lovo.web.mapper.IUserInfoMapper;
import com.lovo.web.mapper.IUserMapper;
import com.lovo.web.service.IUserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    IUserInfoMapper userInfoMapper;
    @Autowired
    ICscUserRemote cscUserRemote;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public UserBean login(String userName, String pwd) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userName);
        qw.eq("u_pwd", pwd);
        UserBean user = userMapper.selectOne(qw);
        if (user.getIsPass()==0) {
            //远程调用审核系统查询用户当前预存款总额对应的折扣
            float discount = cscUserRemote.getDiscount(userName);
            if (user.getDiscount() > discount) {
                user.setNowDiscout(discount);
            } else {
                user.setNowDiscout(user.getDiscount());
            }
            userMapper.updateById(user);
        }
        return user;
    }

    @Override
    public boolean fastRegister(UserBean userBean) throws NoSuchAlgorithmException {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userBean.getUserName());
        UserBean userBean1 = userMapper.selectOne(qw);
        if (userBean1 == null) {
            userBean.setPwd(MD5Encryptor.encrypt(userBean.getPwd()));
            userBean.setRole("用户");
            userMapper.insert(userBean);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean replenishUserInfo(UserBean userBean) throws NoSuchAlgorithmException {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userBean.getUserName());
        UserBean userBean1 = userMapper.selectOne(qw);
        if (userBean1 == null) {
            //MD5加密密码
            userBean.setPwd(MD5Encryptor.encrypt(userBean.getPwd()));
            //设置用户角色
            userBean.setRole("用户");
            //调用工具包压缩图片
            userBean.setAptitudeImg(ImageysUtils.resizeImageTo500K(userBean.getAptitudeImg()));
            userBean.setIdImage(ImageysUtils.resizeImageTo500K(userBean.getIdImage()));
            userMapper.insert(userBean);
            //添加用户信息表信息
            UserInfoBean userInfoBean = new UserInfoBean(userBean.getUserId());

            userInfoMapper.insert(userInfoBean);
            userBean.setUserInfo(userInfoBean);
            String userJson = JSON.toJSONString(userBean);
            rabbitTemplate.convertAndSend("directExchange", "replenishUserInfo.key", userJson);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void replenishInfo(UserBean userBean) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userBean.getUserName());
        UserBean userBean1 = userMapper.selectOne(qw);
        //调用工具包压缩图片
        userBean1.setAptitudeImg(ImageysUtils.resizeImageTo500K(userBean.getAptitudeImg()));
        userBean1.setIdImage(ImageysUtils.resizeImageTo500K(userBean.getIdImage()));
        userBean1.setFirm(userBean.getFirm());
        userBean1.setAuditState(0);
        userMapper.updateById(userBean1);
        //添加用户信息表信息
        UserInfoBean userInfoBean = new UserInfoBean(userBean1.getUserId());
        userInfoMapper.insert(userInfoBean);
        userBean.setUserInfo(userInfoBean);
        String userJson = JSON.toJSONString(userBean);
        rabbitTemplate.convertAndSend("directExchange", "replenishUserInfo.key", userJson);
    }

    @Override
    public UserBean findByUserName(String userName) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userName);
        UserBean user = userMapper.selectOne(qw);
        //远程调用审核系统查询用户当前预存款总额对应的折扣
        float discount = cscUserRemote.getDiscount(userName);
        if (user.getDiscount() > discount) {
            user.setNowDiscout(discount);
        } else {
            user.setNowDiscout(user.getDiscount());
        }
        userMapper.updateById(user);
        return user;
    }

    @Override
    public void updateUserAuditState(String userName, Integer isPass, Integer auditState,
                                     String content, String adminName, LocalDate updateDate) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userName);
        UserBean userBean = userMapper.selectOne(qw);
        userBean.setIsPass(isPass);
        userBean.setAuditState(auditState);
        userMapper.updateById(userBean);
        QueryWrapper<UserInfoBean> qw1 = new QueryWrapper<>();
        qw1.eq("fk_userId", userBean.getUserId());
        qw1.orderByDesc("pk_uiId").last("limit 1");
        UserInfoBean userInfoBean = userInfoMapper.selectOne(qw1);
        userInfoBean.setContent(content);
        userInfoBean.setAdminName(adminName);
        userInfoBean.setUpdateDate(updateDate);
        userInfoMapper.updateById(userInfoBean);

    }

    @Override
    public void updateUserFreezeStatus(String userName, Integer frozenState, String cause) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userName);
        UserBean userBean = userMapper.selectOne(qw);
        userBean.setFrozenState(frozenState);
        userBean.setFrozenCause(cause);
        userMapper.updateById(userBean);
    }

    @Override
    public void addMoney(String userName, float money) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userName);
        UserBean userBean = userMapper.selectOne(qw);
        userBean.setMoney(userBean.getMoney() + money);
        userMapper.updateById(userBean);
    }
}
